package com.dirtylily.myblog.controller;

import com.dirtylily.myblog.dao.BlogDao;
import com.dirtylily.myblog.pojo.*;
import com.dirtylily.myblog.service.BlogService;
import com.dirtylily.myblog.service.TagService;
import com.dirtylily.myblog.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    BlogService blogService;
    @Autowired
    BlogDao blogDao;
    @Autowired
    TypeService typeService;
    @Autowired
    TagService tagService;

//    @GetMapping("/blogs")
//    public String list(){
//        return "admin/blogs";
//    }

    //管理员界面的博客展示、查询工作
    @RequestMapping("/blogsShow")
    public String blogs(Model model,SearchBlog searchBlog, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,HttpSession session){

        //按照排序字段 倒序 排序
        boolean flag = true;
        String orderBy = "b.id desc";

        //pageHelper 进行分页 注意 pageHelper要放在查询语句上
        PageHelper.startPage(pageNum, 8, orderBy);

        List<BlogQuery> list = blogService.getBlogBySearch(searchBlog);


                System.out.println("进入了blogs");
                //list为空的话说明执行的不是查询方法
                if(list==null) {
                    list = blogService.getAllBlogQuery();
                }
            System.out.println(list);
                PageInfo<BlogQuery> pageInfo = new PageInfo<BlogQuery>(list);
                model.addAttribute("types", typeService.getAllType());
                System.out.println(typeService.getAllType());
                model.addAttribute("pageInfo", pageInfo);
        return "admin/blogs";
    }

    //博客新增
    @PostMapping("/blogsSave")
    public String post(blog blog, RedirectAttributes attributes,Model model, HttpSession session){
        //新增的时候需要传递blog对象，blog对象需要有user
        blog.setUser((User) session.getAttribute("user"));
//        String tagIds = (String) model.getAttribute("tagIds");

        //设置blog的type
        blog.setType(typeService.getType(blog.getTypeId()));
        //设置blog中typeId属性
        blog.setTypeId(blog.getTypeId());
        //设置用户id
        blog.setUserId(blog.getUser().getId());
        int b ;
        blog.setTags(tagService.convertToList(blog.getTagId()));
        if(blog.getId()!=null){
           b= blogService.updateBlog(new ShowBlog(blog.getId(),blog.getFlag(),blog.getTitle(),blog.getContent()
                    ,blog.getTypeId(),blog.getFirstPicture(),blog.isRecommend(),
                    blog.isShareStatement(),blog.isAppreciation(),blog.isCommentabled(),blog.isPublished(),blog.getDescription(),blog.getUpdateTime(),blog.getTagId()));
        }else {
             b = blogService.saveBlog(blog);
        }
        if(b == 0){
            attributes.addFlashAttribute("message", "新增失败");
        }else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/blogsShow";
    }

    @PostMapping("/blogs/search")
    public String search(SearchBlog searchBlog, Model model,
                         @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,HttpSession session) {
        List<BlogQuery> blogBySearch = blogService.getBlogBySearch(searchBlog);
        PageHelper.startPage(pageNum, 10,"update_time desc");
        PageInfo<BlogQuery> pageInfo = new PageInfo<>(blogBySearch);

        model.addAttribute("pageInfo", pageInfo);
        //测试用
        Object pageInfo1 = model.getAttribute("pageInfo");
        System.out.println(pageInfo1);
        //只刷新blogList模块
        return "admin/blogs :: blogList";
    }


    @GetMapping("/blogs/input")
    public String input(Model model) {
        //创建新博客的方法
        model.addAttribute("types",typeService.getAllType());
        model.addAttribute("blog", new blog());
        model.addAttribute("tags",tagService.getAllTag());
        return "admin/input";
    }

    @GetMapping("/blogs/{id}/input")
    public String edit(@PathVariable Long id, Model model) {

        model.addAttribute("types",typeService.getAllType());
        blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog", blog);
        model.addAttribute("tags",tagService.getAllTag());
        return "admin/input";
    }
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        blogService.delBlog(id);
        attributes.addFlashAttribute("message", "删除成功");
        return  "redirect:/admin/blogsShow";
    }




}
